<template>
  <div>
    <!-- 首页 -->
    <div class="sy" @click="sy">首页</div>
    <!-- 登录页面 -->
    <div class="login">
      <el-form
        label-width="0"
        class="form"
        ref="loginRef"
        :rules="rules"
        :model="login"
      >
        <!-- 用户名输入框 -->
        <el-form-item prop="username">
          <el-input
            prefix-icon="iconfont icon-gerentouxiang"
            v-model="login.username"
          ></el-input>
        </el-form-item>
        <!-- 密码输入框 -->
        <el-form-item prop="password">
          <el-input
            type="password"
            prefix-icon="iconfont icon-suoding"
            v-model="login.password"
          ></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button type="danger" @click="loginTwo">登录</el-button>
          <el-button type="danger" @click="reSet">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 文字1 -->
    <div class="l">
      <img src="../assets/img/16.png" alt="" />
    </div>
    <!-- 文字2 -->
    <div class="c">
      <img src="../assets/img/17.png" alt="" />
    </div>
    <!-- 文字3 -->
    <div class="x">
      <img src="../assets/img/18.png" alt="" />
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 登录
      login: {
        // 用户输入的名字和密码
        username: "",
        password: "",
      },

      rules: {
        // 验证用户名是否合法
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        // 验证密码是否合法
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },

  methods: {
    // 首页
    sy() {
      this.$router.push("/home2");
    },
    // 点击重置按钮 重置登录表单
    reSet() {
      this.$refs.loginRef.resetFields();
    },
    loginTwo() {
      // 点击登录按钮对整个表单进行校验
      // valid表示表单预验证后的结果false或true
      this.$refs.loginRef.validate(async (valid) => {
        if (!valid) return;
        const { data: res } = await this.$http.post("login2", this.login);
        console.log(res);
        if (res.status !== 200) return this.$message.error("登录失败！");
        this.$message.success("登录成功！");
        // 存储token
        window.sessionStorage.setItem("token", res.token);
        // 获得用户信息
        const user = res.message;
        // 存储用户编号
        window.sessionStorage.setItem("id", user.id);
        // 存储用户名
        window.sessionStorage.setItem("username", user.username);
        // 存储用户性别
        window.sessionStorage.setItem("sex", user.sex);
        // 存储用户手机号
        window.sessionStorage.setItem("tel", user.tel);
        // 存储用户生日
        window.sessionStorage.setItem("date", user.date);
        // 存储用户收货地址
        window.sessionStorage.setItem("place", user.place);
        this.$router.push("/user");
      });
    },
  },
};
</script>

<style lang="less" scoped>
.sy {
  position: absolute;
  cursor: pointer;
  top: 700px;
  left: 50px;
  color: #fff;
  background-color: #b71c1c;
  width: 60px;
  height: 30px;
  line-height: 30px;
  text-align: center;
  border-radius: 6px;
  border: 2px solid #b71c1c;
}
.message {
  width: 30px;
  height: 20px;
  border: 1px solid red;
  position: absolute;
  z-index: 9;
}
.login {
  user-select: none;
  position: absolute;
  top: 200px;
  right: 480px;
  width: 450px;
  height: 380px;
  background-color: #fff;
  z-index: 1;
  border: 2px solid #b71c1c;
  border-radius: 10px;
  form {
    position: absolute;
    bottom: 45px;
    box-sizing: border-box;
    width: 100%;
    padding: 0 20px;
  }
  .btns {
    display: flex;
    justify-content: center;
  }
}
.l {
  position: absolute;
  top: -20px;
  left: 20px;
  width: 700px;
  img {
    width: 100%;
  }
}
.c {
  position: absolute;
  top: 105px;
  right: 150px;
  width: 80px;
  img {
    width: 100%;
  }
}
.x {
  position: absolute;
  z-index: 2;
  top: 209px;
  right: 530px;
  width: 160px;
  img {
    width: 100%;
  }
}
</style>